package wx

import (
	"context"
	"encoding/json"
	"log"
	"time"

	"gin.andbridge/core/model"
	"gin.andbridge/global"
)

type Wx struct{}

func (wx *Wx) StoreWxAccessTokenAndTicket(WxAccessTokenAndTicket *model.WxAccessTokenAndTicket, expire time.Duration) {
	context := context.Background()
	wxAccessTokenAndTicket, err := json.Marshal(WxAccessTokenAndTicket)
	err = global.RedisEngine.Set(context, "wxAccessTokenAndTicket", wxAccessTokenAndTicket, expire).Err()
	if err != nil {
		log.Panicf("redis存入wxAccessTokenAndTicket错误:%v", err)
	}
}

func (wx *Wx) LookupWxAccessTokenAndTicket() (*model.WxAccessTokenAndTicket, error) {
	context := context.Background()

	wxAccessTokenAndTicket, err := global.RedisEngine.Get(context, "wxAccessTokenAndTicket").Result()
	if err != nil {
		return nil, err
	}

	WxAccessTokenAndTicket := &model.WxAccessTokenAndTicket{}
	err = json.Unmarshal([]byte(wxAccessTokenAndTicket), WxAccessTokenAndTicket)

	if err != nil {
		return nil, err
	}
	return WxAccessTokenAndTicket, nil
}
